﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PreguntaAlExperto.RulesLayer.Entities;
using PreguntaAlExperto.RulesLayer.Services;
using System.Net.Mail;
using System.Net;

namespace PreguntaAlExperto
{
    public partial class registrarse : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblEmail.Text = Resources.language.UserEmail;
            this.lblName.Text = Resources.language.NameOfUser;
            this.lblPassword.Text = Resources.language.Password;
            this.lblRepeatPassword.Text = Resources.language.RepeatPassword;
            this.lblUserName.Text = Resources.language.UserName;
            this.btnRegister.Text = Resources.language.RegisterLink;
            this.lblRegisterAsUser.Text = Resources.language.RegisterAsUser;
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtName.Text.Trim()) &&
                !string.IsNullOrEmpty(this.txtPassword.Text.Trim()) &&
                !string.IsNullOrEmpty(this.txtUserName.Text.Trim()) &&
                !string.IsNullOrEmpty(this.txtEmail.Text.Trim()))
            {
                if (this.txtPassword.Text.Trim().Length >= 6 &&
                    this.txtPassword.Text.Trim().Length <= 20)
                {
                    if (this.txtPassword.Text == this.txtRepeatPassword.Text)
                    {
                        if (this.txtName.Text.Trim().Length > 0 &&
                            this.txtName.Text.Trim().Length <= 90)
                        {
                            if (this.txtUserName.Text.Trim().Length > 0 &&
                                this.txtUserName.Text.Trim().Length <= 20)
                            {
                                Publisher _publisher = new Publisher();
                                _publisher.Name = this.txtName.Text;
                                _publisher.Password = this.txtPassword.Text;
                                _publisher.Username = this.txtUserName.Text;
                                _publisher.Email = this.txtEmail.Text;
                                _publisher.EntityState = PreguntaAlExperto.RulesLayer.Base.enuEntityState.Insert;

                                PublisherService _service = new PublisherService();
                                try
                                {
                                    _service.registerNewUser(_publisher);
                                    //Session.Add("UserID", _publisher.ID_Publisher);
                                    //Session.Add("UserName", _publisher.Name);
                                    //Session.Add("UserLevel", false);
                                    //Session.Add("LoginName", this.txtUserName.Text);

                                    Response.Redirect("~/default.aspx");
                                }
                                catch (Exception ex)
                                {
                                    this.lblError.Text = ex.Message;
                                }
                            }
                            else
                            {
                                this.lblError.Text = "El nombre de usuario no es valido.";
                            }
                        }
                        else
                        {
                            this.lblError.Text = "Verifique el nombre.";
                        }
                    }
                    else
                    {
                        this.lblError.Text = "Verifique su contraseña. Puede haber cometido un error al escribirla.";
                    }
                }
                else
                {
                    this.lblError.Text = "La contraseña debe ser mayor o igual a 6 caracteres y menors o igual 20 caracteres.";
                }

            }
            else
            {
                this.lblError.Text = "Faltan datos. Revise los campos y vuelva a intentarlo.";
            }
        }
    }
}
